package com.example.last.controller;
;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.example.last.common.Result;
import com.example.last.common.enums.ErrorCode;
import com.example.last.entity.Paper;
import com.example.last.entity.PaperQuestions;
import com.example.last.entity.Params;
import com.example.last.entity.Questions;
import com.example.last.exception.CustomException;
import com.example.last.service.PaperQuestionsService;
import com.example.last.service.PaperService;
import com.example.last.service.QuestionsService;
import com.example.last.service.TypeService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@CrossOrigin
@RequestMapping("/paper")
@RestController
public class PaperController {
    @Resource
    private PaperService paperService;
    @Resource
    private PaperQuestionsService paperQuestionService;
    @Resource
    private TypeService typeService;
    @Resource
    private QuestionsService questionsService;
    //图书信息查询
    @GetMapping("/search")
    public Result findBySearch(@Param("params") Params params){
        PageInfo<Paper> info = paperService.findBySearch(params);
        return Result.success(info);
    }
    //新增试卷信息
    @PostMapping
    public Result save(@RequestBody Paper paper){
        if(paper.getId() == null){
            paperService.add(paper);
        }else{
            paperService.update(paper);
        }
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getQuestion(@PathVariable Integer id){
        List<Questions> questionList = paperService.findQuestionById(id);
        return Result.success(questionList);
    }

    //删除试卷信息
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        paperService.delete(id);
        return Result.success();
    }

    //批量删除
    @PutMapping("/batch")
    public Result batchdel(@RequestBody List<Paper> list){
        for(Paper paper : list){
            paperService.delete(paper.getId());
        }
        return Result.success();
    }

//    @PostMapping("takePaper")
//    public Result takePaper(@RequestBody PaperDTO paperDTO){
//        List<Questions> listQuestions = questionsService.findByTypeId(paperDTO.getTypeId());
//        if(listQuestions.size() < paperDTO.getNum()) {
//            throw new CustomException(ErrorCode.ERROR_EXIT);
//        }
//        //随机组卷
//        int size = listQuestions.size();
//        List<PaperQuestions> list = new ArrayList<>();
//        List<Integer> typeRandomList = getEleList(size, paperDTO.getNum());
//        for(Integer index : typeRandomList) {
//            Questions questions = listQuestions.get(index);
//            PaperQuestions paperQuestions = new PaperQuestions();
//            paperQuestions.setPaperId(1);
//            paperQuestions.setQuestionsId(questions.getId());
//            list.add(paperQuestions);
//        }
//        paperQuestionService.save(list);
//
//        return Result.success();
//    }

    @PostMapping("takePaper")
    public Result takePaper(@RequestBody Paper paper){
        List<Questions> listQuestions = questionsService.findByTypeId(paper.getTypeId());
        if(listQuestions.size() < paper.getNum()) {
            throw new CustomException(ErrorCode.ERROR_EXIT);
        }
        //随机组卷
        int size = listQuestions.size();
        List<Questions> list = new ArrayList<>();
        List<Integer> typeRandomList = getEleList(size, paper.getNum());
        for(Integer index : typeRandomList) {
            Questions questions = listQuestions.get(index);
            list.add(questions);
        }
        paper.setQuestion(list);
        paperService.add(paper);
        return Result.success();
    }

    @PostMapping("handTake")
    public Result handTake(@RequestBody Paper paper) {
        List<Questions> list = new ArrayList<>();
        List<Integer> typeList = paper.getQuestionId();
        for(Integer index : typeList) {
            Questions questions = questionsService.selectById(index);
            list.add(questions);
        }
        paper.setQuestion(list);
        paperService.add(paper);
        return Result.success();
    }


    //封装一个获取随机数的方法
    private List<Integer> getEleList(int sourceSize, int resultSize) {
        List<Integer> list = CollUtil.newArrayList();
        for(int i = 0; i < sourceSize; i++) {
            list.add(i);
        }
        return RandomUtil.randomEleList(list,resultSize);
    }

    @GetMapping("/findAll")
    private Result selectAll() {
        List<Paper> list = paperService.findAll();
        return Result.success(list);
    }

}
